﻿using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using Moq;
using SmarterCharger.App.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmarterCharger.App.Tests.Unit.FirstTimeUserViewModelSpecifications
{
    public abstract class WhenPasswordChanged : FirstTimeUserViewModelTestBase
    {
        protected virtual string Password { get { return "dummy-password"; } }

        protected override void Act()
        {
            ViewModel.Password = Password;
        }

        [TestMethod]
        public void ThenUsernameShouldEqualSetValue()
        {
            Assert.AreEqual(Password, ViewModel.Password);
        }
    }

    [TestClass]
    public class WhenPasswordChangedGivenPasswordIsEmpty : WhenPasswordChanged
    {
        protected override string Password
        {
            get
            {
                return string.Empty;
            }
        }

        [TestMethod]
        public void ThenSmartPlugsShouldNotBeLoaded()
        {
            SmartPlugLoader.Verify(s => s.Load(ViewModel.Username, Password), Times.Never());
            Assert.IsFalse(ViewModel.IsLoading);
        }

        [TestMethod]
        public void ThenCredentialsShouldBeMarkedInvalid()
        {
            Assert.IsFalse(ViewModel.IsCredentialsValid);
        }
    }

    [TestClass]
    public class WhenPasswordChangedGivenUsernameIsNotEmptyButUsernameIs : WhenPasswordChanged
    {
        protected override void Arrange()
        {
            base.Arrange();
            ViewModel.Username = string.Empty;
        }

        [TestMethod]
        public void ThenSmartPlugsShouldNotBeLoaded()
        {
            SmartPlugLoader.Verify(s => s.Load(ViewModel.Username, Password), Times.Never());
            Assert.IsFalse(ViewModel.IsLoading);
        }

        [TestMethod]
        public void ThenCredentialsShouldBeMarkedInvalid()
        {
            Assert.IsFalse(ViewModel.IsCredentialsValid);
        }
    }

    [TestClass]
    public class WhenPasswordChangedGivenUsernameAndPasswordAreNotEmptyButIsLoading : WhenPasswordChanged
    {
        protected override void Arrange()
        {
            base.Arrange();
            ViewModel.Username = "dummy-username";
            ViewModel.IsLoading = true;
        }

        [TestMethod]
        public void ThenSmartPlugsShouldNotBeLoaded()
        {
            SmartPlugLoader.Verify(s => s.Load(ViewModel.Username, Password), Times.Never());
        }

        [TestMethod]
        public void ThenCredentialsShouldBeMarkedInvalid()
        {
            Assert.IsFalse(ViewModel.IsCredentialsValid);
        }
    }

    [TestClass]
    public class WhenPasswordChangedGivenUsernameAndPasswordAreNotEmptyAndIsNotLoading : WhenPasswordChanged
    {
        protected override void Arrange()
        {
            base.Arrange();
            ViewModel.Username = "dummy-username";
            ViewModel.IsLoading = false;
        }

        [TestMethod]
        public void ThenSmartPlugsShouldBeLoaded()
        {
            SmartPlugLoader.Verify(s => s.Load(ViewModel.Username, Password), Times.Once());
            Assert.IsFalse(ViewModel.IsLoading);
        }

        [TestMethod]
        public void ThenCredentialsShouldBeMarkedValid()
        {
            Assert.IsTrue(ViewModel.IsCredentialsValid);
        }
    }
}